<?php
class Default_Form_Validate_UserGroup_UserGroup{
	
	//CHUA NHUNG THONG BAO LOI CUA FORM
	protected $_messagesError = null;
	
	//MANG CHUA DU LIEU SAU KHI KIEM TRA
	protected  $_arrData;
	
	public function __construct($arrParam = array(), $options = null){
		//=========================================
		//KIEM TRA GROUP NAME
		//==========================================
		if($arrParam['action'] == 'add'){
			$option    = array('table'=>'user_group','field'=>'group_name');
		}else if($arrParam['action'] == 'edit'){
			$clause = 'id != '. $arrParam['id'];
			$option    = array('table'=>'user_group','field'=>'group_name',
								'exclude'=>$clause);
		}
		$validator = new Zend_Validate();
		$validator->addValidator(new Zendvn_Validate_NotEmpty(),true)
				  ->addValidator(new Zendvn_Validate_StringLength(4,32))
				  ->addValidator(new Zendvn_Validate_Regex('#^[a-zA-Z0-9\-_\s\.]+$#'),true)
				  ->addValidator(new Zendvn_Validate_Db_NoRecordExists($option),true);
		if(!$validator->isValid($arrParam['group_name'])){
			$message = $validator->getMessages();
			$this->_messagesError['group_name'] = 'Tên nhóm: '.current($message);
			$arrParam['group_name']='';
		}
		
		//=========================================
		//KIEM TRA GROUP_ACP
		//==========================================
		if($arrParam['group_acp'] == 2){
			$this->_messagesError['group_acp'] = 'Thuộc nhóm Admin chưa chọn';
		}
		//=========================================
		//KIEM TRA Status
		//==========================================
		if($arrParam['status'] == 2){
			$this->_messagesError['status'] = 'Chưa chọn trạng thái';
		}
		
		
		//=========================================
		//TRUYEN CAC GIA TRI DUNG VAO $_arrData
		//==========================================
		$this->_arrData = $arrParam;
	}
	
	//KIEM TRA LOI
	//return true neu co loi xay ra
	public function isError(){
		if(count($this->_messagesError)>0){
			return true;
		}else{
			return false;
		}
		
	}
	
	//TRA VE MOT MANG CAC LOI
	public function getMessageError(){
		return $this->_messagesError;
	}
	
	//TRA VE MOT MANG DU LIEU SAU KHI KIEM TRA
	public function getData($options = null){
		return $this->_arrData;
	}
	
}